home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Tapping Into the Keyboard / TypeAway / Caret.cs next >
Encoding:
Text File  |  2001-01-15  |  4.0 KB  |  133 lines

  1. //------------------------------------
  2. // Caret.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Runtime.InteropServices;
  7. using System.Windows.Forms;
  8.  
  9. namespace Petzold.ProgrammingWindowsWithCSharp
  10. {
  11.      class Caret
  12.      {
  13.           [DllImport("user32.dll")]
  14.           public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, 
  15.                                                int cx, int cy);
  16.           [DllImport("user32.dll")]
  17.           public static extern int DestroyCaret();
  18.  
  19.           [DllImport("user32.dll")]
  20.           public static extern int SetCaretPos(int x, int y);
  21.  
  22.           [DllImport("user32.dll")]
  23.           public static extern int ShowCaret(IntPtr hwnd);
  24.  
  25.           [DllImport("user32.dll")]
  26.           public static extern int HideCaret(IntPtr hwnd);
  27.                                                             // Fields
  28.           Control ctrl;
  29.           Size    size;
  30.           Point   ptPos;
  31.           bool    bVisible;
  32.                                                             // Constructors
  33.                // Don't allow default constructor.
  34.  
  35.           private Caret()
  36.           {
  37.           }
  38.                // Only allowable constructor has Control argument.
  39.  
  40.           public Caret(Control ctrl)
  41.           {
  42.                this.ctrl = ctrl;
  43.                Position  = Point.Empty;
  44.                Size      = new Size(1, ctrl.Font.Height);
  45.  
  46.                Control.GotFocus  += new EventHandler(ControlOnGotFocus);
  47.                Control.LostFocus += new EventHandler(ControlOnLostFocus);
  48.  
  49.                     // If the control already has focus, create the caret.
  50.  
  51.                if (ctrl.Focused)
  52.                     ControlOnGotFocus(ctrl, new EventArgs());
  53.           }
  54.                                                             // Properties
  55.           public Control Control
  56.           {
  57.                get 
  58.                {
  59.                     return ctrl;
  60.                }
  61.           }
  62.           public Size Size
  63.           {
  64.                get 
  65.                {
  66.                     return size;
  67.                }
  68.                set
  69.                {
  70.                     size = value;
  71.                }
  72.           }
  73.           public Point Position
  74.           {
  75.                get
  76.                {
  77.                     return ptPos;
  78.                }
  79.                set
  80.                {
  81.                     ptPos = value;
  82.                     SetCaretPos(ptPos.X, ptPos.Y);
  83.                }
  84.           }
  85.           public bool Visibility
  86.           {
  87.                get
  88.                {
  89.                     return bVisible;
  90.                }
  91.                set
  92.                {
  93.                     if (bVisible = value)
  94.                          ShowCaret(Control.Handle);
  95.                     else
  96.                          HideCaret(Control.Handle);
  97.                }
  98.           }
  99.                                                             // Methods
  100.           public void Show()
  101.           {
  102.                Visibility = true;
  103.           }
  104.           public void Hide()
  105.           {
  106.                Visibility = false;
  107.           }
  108.           public void Dispose()
  109.           {
  110.                     // If the control has focus, destroy the caret.
  111.  
  112.                if (ctrl.Focused)
  113.                     ControlOnLostFocus(ctrl, new EventArgs());
  114.  
  115.                Control.GotFocus  -= new EventHandler(ControlOnGotFocus);
  116.                Control.LostFocus -= new EventHandler(ControlOnLostFocus);
  117.           }
  118.                                                             // Event handlers
  119.           void ControlOnGotFocus(object obj, EventArgs ea)
  120.           {
  121.                CreateCaret(Control.Handle, IntPtr.Zero, 
  122.                            Size.Width, Size.Height);
  123.                SetCaretPos(Position.X, Position.Y);
  124.                Show();
  125.           }
  126.           void ControlOnLostFocus(object obj, EventArgs ea)
  127.           {
  128.                Hide();
  129.                DestroyCaret();
  130.           }
  131.      }
  132. }
  133.